package com.uit.se6.mydiary;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.uit.se6.mydiary.custom.CustomEditText;
import com.uit.se6.mydiary.custom.CustomWindow;
import com.uit.se6.mydiary.custom.HorizontalImageAdapter;
import com.uit.se6.mydiary.custom.HorizontalView;
import com.uit.se6.mydiary.util.EmotionText;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

public class DiaryEdit extends BaseActivity {
	private TextView mtxtNameDiary;
	private CustomEditText editNote;
	private String format;
	//----list image emotion
	private List<Drawable> drawables;
	private HorizontalImageAdapter imageAdapter;
	private HorizontalView listView;
		
	//----create camera----//
	private static final String TAG = "CallCamera";
	private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
	private ImageView btnCamera;
	Uri fileUri = null;
	ImageView photoImage = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.diary_edit);
		mtxtNameDiary = (TextView) findViewById(R.id.namediary);
		format = new String("EEEE, MMMM dd, yyyy");		
		listView = (HorizontalView) findViewById(R.id.viewicon);
		editNote = (CustomEditText) findViewById(R.id.editNote);
		btnCamera = (ImageView) findViewById(R.id.btncamera);
		LoadTabs();//tab menu
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
		mtxtNameDiary.setText(sdf.format(new java.util.Date()));
		getDrawablesList();
		setupUI();
		//----create camera----//
		btnCamera.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				File file = getOutputPhotoFile();
		        fileUri = Uri.fromFile(getOutputPhotoFile());
		        i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
		        startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
			}
		});

	}
	//-----list image emotion set up----//
	private void setupUI() {
		imageAdapter = new HorizontalImageAdapter(this, drawables);
		listView.setAdapter(imageAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View item,
					int position, long id) {
				String emotionText = getStringEmotionForId(position);
				String noteText = editNote.getText() + emotionText;
				editNote.setText(EmotionText.getSmiledText(DiaryEdit.this,
						noteText, 30));
				editNote.setSelection(editNote.length());
			}
		});
	}
	private File getOutputPhotoFile() {
		  File directory = new File(Environment.getExternalStoragePublicDirectory(
		                Environment.DIRECTORY_PICTURES), getPackageName());
		  if (!directory.exists()) {
		    if (!directory.mkdirs()) {
		      Log.e(TAG, "Failed to create storage directory.");
		      return null;
		    }
		  }
		  String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.UK).format(new java.util.Date());
		  return new File(directory.getPath() + File.separator + "IMG_"  
		                    + timeStamp + ".jpg");
		}

	private String getStringEmotionForId(int id) {
		String symbol = "*";
		String emotionText = "";
		switch (id) {
		case 0:
			emotionText = ":)";
			break;
		case 1:
			emotionText = ":|";
			break;
		case 2:
			emotionText = "oo";
			break;
		case 3:
			emotionText = "ooo";
			break;
		case 4:
			emotionText = "X-P";
			break;
		case 5:
			emotionText = "X-|";
			break;
		case 6:
			emotionText = "=.=";
			break;
		case 7:
			emotionText = "XD";
			break;
		case 8:
			emotionText = ":D";
			break;
		case 9:
			emotionText = ":'>";
			break;
		case 10:
			emotionText = ":-O";
			break;
		case 11:
			emotionText = ":-PP";
			break;
		case 12:
			emotionText = ":((";
			break;
		case 13:
			emotionText = ":((o";
			break;
		case 14:
			emotionText = ":DD";
			break;
		case 15:
			emotionText = "X-((";
			break;
		case 16:
			emotionText = "XP";
			break;
		case 17:
			emotionText = ":-((";
			break;
		case 18:
			emotionText = ":X";
			break;
		case 19:
			emotionText = ":))";
			break;
		case 20:
			emotionText = "::X";
			break;
		case 21:
			emotionText = ":(";
			break;
		case 22:
			emotionText = "=-";
			break;
		case 23:
			emotionText = ":)";
			break;

		default:
			break;
		}
		return (symbol + emotionText + symbol);
	}

	private void getDrawablesList() {
		drawables = new ArrayList<Drawable>();
		drawables.add(getResources().getDrawable(R.drawable.e1));
		drawables.add(getResources().getDrawable(R.drawable.e2));
		drawables.add(getResources().getDrawable(R.drawable.e3));
		drawables.add(getResources().getDrawable(R.drawable.e4));
		drawables.add(getResources().getDrawable(R.drawable.e5));
		drawables.add(getResources().getDrawable(R.drawable.e_angry));
		drawables.add(getResources().getDrawable(R.drawable.e_annoyed));
		drawables.add(getResources().getDrawable(R.drawable.e_biggrin));
		drawables.add(getResources().getDrawable(R.drawable.e_biggrin_eye));
		drawables.add(getResources().getDrawable(R.drawable.e_blushing));
		drawables.add(getResources().getDrawable(R.drawable.e_confused));
		drawables.add(getResources().getDrawable(R.drawable.e_crazy));
		drawables.add(getResources().getDrawable(R.drawable.e_cry));
		drawables.add(getResources().getDrawable(R.drawable.e_cry_scream));
		drawables.add(getResources().getDrawable(R.drawable.e_excited));
		drawables.add(getResources().getDrawable(R.drawable.e_fierce));
		drawables.add(getResources().getDrawable(R.drawable.e_hungry));
		drawables.add(getResources().getDrawable(R.drawable.e_hurt));
		drawables.add(getResources().getDrawable(R.drawable.e_kiss));
		drawables.add(getResources().getDrawable(R.drawable.e_laugh));
		drawables.add(getResources().getDrawable(R.drawable.e_lovestruck));
		drawables.add(getResources().getDrawable(R.drawable.e_sad));
		drawables.add(getResources().getDrawable(R.drawable.e_raisedeyebrow));
		drawables.add(getResources().getDrawable(R.drawable.e_scared));
	}

	public void LoadTabs() {
		final TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
		tab.setup();
		TabHost.TabSpec spec;
		// tao tab 1
		spec = tab.newTabSpec("tabCamera");
		spec.setContent(R.id.tabcamera);
		spec.setIndicator("Camera");

		tab.addTab(spec);
		// tao tab 2
		spec = tab.newTabSpec("tabIcon");
		spec.setContent(R.id.tabicon);
		spec.setIndicator("Icon");
		tab.addTab(spec);
		tab.setCurrentTab(0);

		tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String arg0) {

			}
		});

	}
}
